﻿using GeneralTool.CoreLibrary.Interfaces;

namespace GeneralTool.CoreLibrary.IniHelpers
{
    /// <summary>
    /// 配置项
    /// </summary>
    public abstract class Category
    {
        #region Public 构造函数

        /// <summary>
        /// </summary>
        /// <param name="sectionName">
        /// </param>
        public Category(string sectionName)
        {
            SectionName = sectionName;

        }

        #endregion Public 构造函数

        #region Public 属性

        /// <summary>
        /// 节点名称
        /// </summary>
        public string SectionName { get; set; }

        #endregion Public 属性

    }



    /// <summary>
    /// 节点
    /// </summary>
    /// <typeparam name="T">
    /// </typeparam>
    public class Node<T>
    {
        #region Public 构造函数

        /// <summary>
        /// </summary>
        /// <param name="sectionName">
        /// 节点名称
        /// </param>
        /// <param name="keyName">
        /// 键名
        /// </param>
        /// <param name="defaultValue">
        /// 默认值
        /// </param>
        /// <param name="create">是否在空时自动创建默认值</param>
        /// <param name="iniPath">单独保存到的文件路径,如无,则使用 Configs/default.ini</param>
        /// <param name="isJosn">是否Json</param>
        /// <param name="convert">Json的转换器,如无则使用默认转换器</param>
        public Node(string sectionName, string keyName, T defaultValue, bool create = false, string iniPath = "", bool isJosn = false, IJsonConvert convert = null)
        {
            this.SectionName = sectionName;
            this.KeyName = keyName;

            this.DefaultValue = defaultValue;
            this.IniPath = iniPath;

            this.IsJosn = isJosn;
            this.JsonConvert = convert;


            if (create)
            {
                string tmp = this.IniHelper.GetValue<string>(SectionName, KeyName);
                if (string.IsNullOrWhiteSpace(tmp))
                {
                    Value = defaultValue;
                }
            }
        }

        #endregion Public 构造函数

        #region Public 属性

        /// <summary>
        /// 是否为Json类型
        /// </summary>
        public bool IsJosn { get; set; }

        /// <summary>
        /// Json的转换器
        /// </summary>
        public IJsonConvert JsonConvert { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public IniHelper IniHelper { get; set; }

        private string iniPath;
        /// <summary>
        /// 文件路径
        /// </summary>
        public string IniPath
        {
            get => iniPath;
            set
            {
                IniHelper = string.IsNullOrEmpty(value) ? IniHelper.IniHelperInstance : new IniHelper(value);
                iniPath = IniHelper.Path;
            }
        }

        /// <summary>
        /// 当前默认值
        /// </summary>
        public T DefaultValue { get; set; }

        /// <summary>
        /// 当前键名称
        /// </summary>
        public string KeyName { get; set; }

        /// <summary>
        /// 当前节点的所属
        /// </summary>
        public string SectionName { get; set; }

        /// <summary>
        /// 当前值
        /// </summary>
        public T Value
        {
            get
            {
                var val = SimpleIniManager.GetValue(typeof(T), this.IniHelper, this.SectionName, this.KeyName, this.DefaultValue, this.IsJosn, this.JsonConvert);
                return (T)val;
            }
            set
            {
                SimpleIniManager.SetValue(this.IniHelper, this.SectionName, this.KeyName, value, typeof(T), this.IsJosn, this.JsonConvert);
            }
        }



        #endregion Public 属性

        #region Public 方法

        /// <summary>
        /// 转换
        /// </summary>
        /// <param name="value">
        /// </param>
        public static implicit operator T(Node<T> value)
        {
            return value.Value;
        }

        #endregion Public 方法
    }
}